/*
 * @lc app=leetcode.cn id=59 lang=cpp
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> matrix(n,vector<int>(n,0));
        int top = 0;
        int bottom = n - 1;
        int left = 0;
        int right = n - 1;

        int TotalNum = 1 ;
        while(TotalNum <= n * n)
        {
            for(int i = left ; i <= right && TotalNum <= n * n ; i++)
            {
                matrix[top][i] = TotalNum;
                TotalNum++;
            }
            top++;

            for(int i = top ; i <= bottom && TotalNum <= n * n ; i++)
            {
                matrix[i][right] = TotalNum;
                TotalNum++;
            }
            right--;

            for(int i = right; i >= left && TotalNum <= n * n ; i--)
            {
                matrix[bottom][i] = TotalNum;
                TotalNum++;
            }
            bottom--;

            for(int i = bottom; i >= top && TotalNum <= n * n ; i--)
            {
                matrix[i][left] = TotalNum;
                TotalNum++;
            }    
            left++;
        }

    return matrix;
        

    }
};
// @lc code=end

